package com.oreilly.android.taskmanager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.oreilly.android.taskmanager.tasks.Task;
import com.oreilly.android.taskmanager.views.AddressOverlay;

public class ShowTasksMapActivity extends MapActivity {

	private MapView mapView;
	private TaskManagerApplication app;

	private MyLocationOverlay myLocationOverlay;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.show_tasks_map);

		app = (TaskManagerApplication) getApplication();

		setUpViews();
	}

	@Override
	protected void onResume() {
		super.onResume();
		boolean myLocationAvailable = myLocationOverlay.enableMyLocation();
		if (myLocationAvailable) {
			updateTaskLocations();
		} else {
			Toast toast = new Toast(this);
			toast.setText("Location not avilable");
			toast.show();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		myLocationOverlay.disableMyLocation();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private void setUpViews() {
		mapView = (MapView) findViewById(R.id.show_tasks_map);
		mapView.setBuiltInZoomControls(true);

		myLocationOverlay = new MyLocationOverlay(this, mapView);
		// Spinner spinner = (Spinner) findViewById(R.id.tasks_spinner);
		// spinner.setAdapter();
		// spinner.setOnItemClickListener(new AdapterView.OnItemClickListener()
		// {
		//
		// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
		// long arg3) {
		// // TODO Auto-generated method stub
		//
		// }
		// });
	}

	private void updateTaskLocations() {
		int MAX_RESULTS = 1;
		Geocoder geo = new Geocoder(this);
		ArrayList<Task> tasks = app.getAllTasks();
		List<Address> addresses;
		List<Overlay> mapOverlays = mapView.getOverlays();
		AddressOverlay addressOverlay;
		Drawable greenPushPin = getResources().getDrawable(
				R.drawable.grn_push_pin);
		try {

			addressOverlay = new AddressOverlay(greenPushPin);
			List<Address> adds = new ArrayList<Address>();
			for (Task t : tasks) {
				if (t.hasLocation()) {
					double latitude = t.getLatitude();
					double longitude = t.getLongitude();
					// Log.d("TaskManager", t.toString() + " LL:" + latitude +
					// " "
					// + longitude);
					addresses = geo.getFromLocation(latitude, longitude,
							MAX_RESULTS);
					// error here
					Address add = addresses.get(0);
					adds.add(add);
				}
			}
			addressOverlay.setAddresses(adds);
			mapOverlays.add(addressOverlay);
			mapOverlays.add(myLocationOverlay);
			mapView.invalidate();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
